//
//  ImageListView.m
//  安好
//
//  Created by rlp on 14-11-21.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "ImageListView.h"

@implementation ImageListView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

/**
*  初始化横向图片列表
*
*  @param frame      x,y是关键
*  @param imageViews 图片的地址集合
*
*  @return view
*/
-(instancetype)initWithFrame:(CGRect)frame imageViews:(NSArray *)imageViews{
    self = [super initWithFrame:frame];
    if (self) {
        if (imageViews==nil||imageViews.count==0||[imageViews isEqual:[NSNull null]]) {
            self.frame = CGRectZero;
            return self;
        }
        for (int i=0; i<(imageViews.count>=4?4:imageViews.count); i++) {
            
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((55+23
                                                                                   )*i, 0, 55, 55)];
            imageview.tag = i;
//            imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[imageViews objectAtIndex:i]]];
            [imageview setImageURLStr:[NSString stringWithFormat:@"%@",[imageViews objectAtIndex:i]] placeholder:[UIImage imageNamed:@"公用_默认图片"]];
            
            imageview.layer.masksToBounds = YES;
            imageview.layer.cornerRadius = 3.0;
            imageview.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked:)];
            [imageview addGestureRecognizer:singleTap];
            [self  addSubview:imageview];
            
        }
        self.frame =CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 55);

    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame imageViewsDict:(NSDictionary *)imageViewsDict{
    self = [super initWithFrame:frame];
    if (self) {
        if (imageViewsDict==nil||imageViewsDict.count==0||[imageViewsDict isEqual:[NSNull null]]) {
            self.frame = CGRectZero;
            return self;
        }
        for (int i=0; i<(imageViewsDict.count>=4?4:imageViewsDict.count); i++) {
            
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((55+23
                                                                                   )*i, 0, 55, 55)];
            imageview.tag = i;
            imageview.image = [imageViewsDict objectForKey:[NSString stringWithFormat:@"%d",i]];
//            [imageview setImageURLStr:[NSString stringWithFormat:@"%@",[imageViews objectAtIndex:i]] placeholder:nil];
            
            imageview.layer.masksToBounds = YES;
            imageview.layer.cornerRadius = 3.0;
            imageview.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked:)];
            [imageview addGestureRecognizer:singleTap];
            [self  addSubview:imageview];
            
        }
        self.frame =CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 55);
        
    }
    return self;
}




-(IBAction)imageClicked:(id)sender{
    UITapGestureRecognizer *top = sender;
    NSInteger tags = top.view.tag;
    [self.delegate imageViewsIndex:top.view.superview.tag subImageIndex:tags];
    
}
-(void)setImageViewsDict:(NSMutableDictionary *)imageViewsDict{
    _imageViewsDict = imageViewsDict;
    
    for (int i=0; i<self.subviews.count; i++) {
        UIImageView *imageview =(UIImageView *)[self viewWithTag:i];

        if (i<imageViewsDict.count) {
//            for (int j=0; j<imageViewsDict.count; j++) {
                imageview.image = [imageViewsDict objectForKey:[NSString stringWithFormat:@"%d",i]];
//            }
            }else{
                [imageview removeFromSuperview];
            }
        }
}

//-(instancetype)initWithFrame:(CGRect)frame{
//    self = [super initWithFrame:frame];
//    if (self) {
//    
//    }
//    return self;
//}
-(void)setImages:(NSMutableArray *)images{
    
    _images = images;
    
    if (_images==nil||_images.count==0||[_images isEqual:[NSNull null]]) {
        self.frame = CGRectZero;
        return ;
    }
    
    [UIView animateWithDuration:0.3f animations:^{
        
 
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    
    for (int i=0; i<(_images.count>=4?4:_images.count); i++) {
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((55+23
                                                                               )*i, 0, 55, 55)];
        imageview.tag = i;
        imageview.image = [_images objectAtIndex:i];
        imageview.layer.masksToBounds = YES;
        imageview.layer.cornerRadius = 3.0;
        imageview.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked:)];
        [imageview addGestureRecognizer:singleTap];
        [self  addSubview:imageview];
        
    }
           }];
//    self.frame =CGRectMake(0, 0, self.frame.size.width, 55);


}

@end
